package com.pansoft.openplanet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.chat.widget.BadgeView;
import com.efounder.thirdpartycomps.stickylistheaders.StickyListHeadersAdapter;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.Contact;

import java.util.List;
import java.util.Locale;

/**
 * Created by will on 18-3-21.
 */

public class MobileContactsAdapter extends BaseAdapter implements
        StickyListHeadersAdapter, SectionIndexer {
    private List<Contact> list = null;
    private Context mContext;

    public MobileContactsAdapter(Context mContext, List<Contact> list) {
        this.mContext = mContext;
        this.list = list;
    }

    public void updateListView(List<Contact> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        final Contact user = list.get(position);
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(
                    R.layout.talk_chain_fragment_contacts_item, viewGroup, false);
            viewHolder.text = view.findViewById(R.id.title);
            viewHolder.avatar = view.findViewById(R.id.avatar);
            viewHolder.iv_register = view.findViewById(R.id.iv_isregister);
            viewHolder.iconcontainerforbadage = view.findViewById(R.id.iconcontainerforbadage);
            viewHolder.badge = new BadgeView(mContext, viewHolder.iconcontainerforbadage);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (user.getName() == null) {
            viewHolder.text.setText("未知用户");
        } else {
            viewHolder.text.setText(user.getName());
        }
        viewHolder.badge.setVisibility(View.GONE);
//        String avatarStr = "drawable://";
        int avatarId = R.drawable.contact_default_avatar;
        // TODO 头像 viewHolder.avatar
        // avatarStr = avatarStr + R.drawable.default_useravatar;

        if (user != null && user.getAvatar() != null && user.getAvatar().contains("http")) {
            LXGlideImageLoader.getInstance().showUserAvatar(mContext, viewHolder.avatar, user.getAvatar());
        } else {
            LXGlideImageLoader.getInstance().displayImage(mContext, viewHolder.avatar, avatarId, avatarId, avatarId);
        }

        if (user.isRegister()) {
            viewHolder.iv_register.setVisibility(View.VISIBLE);
        } else {
            viewHolder.iv_register.setVisibility(View.INVISIBLE);
        }
        return view;

    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder holder;

        if (convertView == null) {
            holder = new HeaderViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.contacts_header, parent, false);
            holder.text = convertView.findViewById(R.id.header);

            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }

        // set header text as first char in name
        CharSequence headerChar = list.get(position).getSortLetters();
        holder.text.setText(headerChar);
        // 隐藏最顶端headerView
        if ("↑".equals(headerChar)) {
            holder.text.setVisibility(View.GONE);
        } else {
            holder.text.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    @Override
    public int getSectionForPosition(int position) {
        return list.get(position).getSortLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    @Override
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase(Locale.getDefault()).charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    @Override
    public Object[] getSections() {
        return list.toArray();
    }

    @Override
    public long getHeaderId(int position) {
        return list.get(position).getSortLetters().charAt(0);
    }

    class HeaderViewHolder {
        TextView text;
    }

    class ViewHolder {
        TextView text;
        ImageView avatar;
        LinearLayout iconcontainerforbadage;
        public BadgeView badge;

        ImageView iv_register;
    }

    /**
     * 异步加载头像
     */
//    public void initImageLoader() {
//        // 初始化异步加载图片的类
//        imageLoader = ImageLoader.getInstance();
//        // 设置异步加载图片的配置信息
//        options = ImageUtil.getImageLoaderOptions(R.drawable.default_useravatar);
//
//    }

}
